package com.hngy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hngy.common.CustomException;
import com.hngy.dao.DishDao;
import com.hngy.domain.Category;
import com.hngy.domain.Dish;
import com.hngy.domain.DishFlavor;
import com.hngy.dto.DishDto;
import com.hngy.service.CategoryService;
import com.hngy.service.DishFlavorService;
import com.hngy.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LiKe
 * @date 2021/12/26 16:20
 */
@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    public void add(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        Long dishId = dishDto.getId();//菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public IPage pageDishType(int page, int pageSize, String name) {
        //菜品表的分页构造器
        IPage<Dish> dishIPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);

        this.page(dishIPage,queryWrapper);

        List<Dish> dishList = dishIPage.getRecords();


        List<DishDto> list = dishList.stream().map((item)->{
           //查询菜品对应的分类
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            //创建DishDto
            DishDto dishDto = new DishDto();
            //copy菜品到dto
            BeanUtils.copyProperties(item,dishDto);
            //菜品分类
            dishDto.setCategoryName(category.getName());

            return dishDto;
        }).collect(Collectors.toList());
        IPage<DishDto> dishDtoIPage = new Page<>();
        BeanUtils.copyProperties(dishIPage,dishDtoIPage);
        dishDtoIPage.setRecords(list);
        return dishDtoIPage;
    }

    @Override
    public DishDto getDishDto(Long id) {

        Dish dish = this.getById(id);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());

        List<DishFlavor> list = dishFlavorService.list(queryWrapper);

        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish,dishDto);

        dishDto.setFlavors(list);

        return dishDto;
    }

    @Override
    public void updateDishFlavor(DishDto dishDto) {

        this.updateById(dishDto);
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);
        //再新增

        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());


        dishFlavorService.saveBatch(flavors);


    }

    @Override
    public void deleteById(String[] ids) {

        //判断是否启用状态
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);
        if (count>0){
            throw new CustomException("菜品正在售卖！删除失败！");
        }

        List<String> list = new ArrayList<>();
        for (String id : ids) {
            list.add(id);
        }
        //删除口味
        LambdaQueryWrapper<DishFlavor> wrapper =new LambdaQueryWrapper<>();
        wrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(wrapper);
        //删除菜品
        this.removeByIds(list);


    }

    @Override
    public void updateStatus(int status, List<Long> ids) {
        //修改状态
        //update dish set status = {} in (id,id);
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Dish::getStatus,status);
        updateWrapper.in(Dish::getId,ids);
        this.update(updateWrapper);
    }

    @Override
    public List<Dish> listType(Long categoryId) {

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Dish::getCategoryId,categoryId);
        List<Dish> list = this.list(queryWrapper);
        return list;
    }
}
